package cn.ahaiahaixx.supemarketonline.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.ahaiahaixx.supemarketonline.dao.TbLogsDAO;
import cn.ahaiahaixx.supemarketonline.dao.TbUserDAO;
import cn.ahaiahaixx.supemarketonline.entity.TbLogs;
import cn.ahaiahaixx.supemarketonline.entity.TbTokenInfo;
import cn.ahaiahaixx.supemarketonline.entity.TbUser;
import cn.ahaiahaixx.supemarketonline.model.TbUserModel;
import cn.ahaiahaixx.supemarketonline.service.UserService;
import cn.ahaiahaixx.supemarketonline.utils.JsonMessage;
import cn.ahaiahaixx.supemarketonline.utils.MyUtils;
import cn.ahaiahaixx.supemarketonline.utils.PageBean;

/**
 * 管理用户服务实现
 * 
 * @author Aiden Pearce
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

  @Autowired
  private TbUserDAO tbUserDAO;
  @Autowired
  private TbLogsDAO tbLogsDAO;

  /**
   * 分页查询
   */
  @Override
  public JsonMessage queryAll(TbUserModel model) throws Exception {
    JsonMessage message = JsonMessage.getSuccess("");
    PageBean page = model.getPage();
    PageHelper.startPage(page.getPageNumber(), page.getPageSize());
    Page<TbUser> list = (Page<TbUser>) tbUserDAO.queryAll();
    page.setPageInfo(list);
    message.getDatas().put("page", page);
    message.getDatas().put("list", list);
    return message;
  }

  /**
   * 用户登录
   */
  @Override
  public JsonMessage login(TbUserModel model) throws Exception {
    TbUser user = model.getTbUser();
    if (MyUtils.isEmpty(user.getUsername())) {
      return JsonMessage.getFail("用户名必须填写");
    }
    if (MyUtils.isEmpty(user.getPassword())) {
      return JsonMessage.getFail("密码必须填写");
    }
    TbUser nuser = tbUserDAO.queryByKeyName(user);
    if (nuser == null) {
      return JsonMessage.getFail("用户不存在");
    }
    if (!"y".equalsIgnoreCase(nuser.getIsEnable())) {
      return JsonMessage.getFail("用户已经冻结");
    }
    if (!nuser.getPassword().equalsIgnoreCase(user.getPassword())) {
      return JsonMessage.getFail("密码错误");
    }
    // 登录成功需要添加token
    TbTokenInfo tokenInfo = model.makeTbTokenInfo();
    tokenInfo.setInfo(nuser.getAuid() + "");
    // 查看是否存在token中是否存在
    TbUser tuser = tbUserDAO.queryTokenUser(tokenInfo);
    if (tuser != null) {
      // 存在就删除
      tbUserDAO.deleteTokenUser(tokenInfo);
    }

    System.out.println("=======>" + tokenInfo);
    tbUserDAO.saveUserToken(tokenInfo);
    // 记录登录信息
    TbLogs logs = new TbLogs();
    // 用户id
    logs.setOperator(tokenInfo.getInfo());
    logs.setLogtype("logs");
    logs.setLog("用户登录");
    tbLogsDAO.add(logs);
    return JsonMessage.getSuccess("登录成功");
  }

  /**
   * 查询token信息
   */
  @Override
  public JsonMessage getUserInfo(TbUserModel model) throws Exception {
    JsonMessage message = JsonMessage.getSuccess("");
    TbUser user = tbUserDAO.queryTokenUser(model.makeTbTokenInfo());
    if (user != null) {
      // 去掉敏感信息
      user.setAuid(null);
      user.setPassword(null);
    }
    message.put("user", user);
    return message;
  }

  
  @Override
  public JsonMessage logout(TbUserModel model) throws Exception {
    tbUserDAO.deleteTokenUser(model.makeTbTokenInfo());
    return JsonMessage.getSuccess("登出成功");
  }

  @Override
  public JsonMessage useradd(TbUserModel model) throws Exception {
    TbUser tbUser = model.getTbUser();
    if (MyUtils.isEmpty(tbUser.getUsername())) {
      return JsonMessage.getFail("用户名必须填写");
    }
    if (MyUtils.isEmpty(tbUser.getPassword())) {
      return JsonMessage.getFail("密码必须填写");
    }
    if (MyUtils.isEmpty(tbUser.getNickname())) {
      return JsonMessage.getFail("给自己一个昵称吧，让大家记住你");
    }
    TbUser check = tbUserDAO.queryByKeyName(tbUser);
    TbUser checkn = tbUserDAO.queryByKeyNick(tbUser);
    if (check != null) {
      return JsonMessage.getFail("用户名已经存在了");
    }
    if (checkn != null) {
      return JsonMessage.getFail("你的昵称已经存在");
    }
    int result = tbUserDAO.useradd(model.getTbUser());
    // 添加日志
    TbLogs logs = new TbLogs();
    // 类型
    logs.setLogtype("logs");
    // 信息
    logs.setLog(String.format("添加了:%s", model.getLoginAdminUser()));
    tbLogsDAO.add(logs);

    return result == 1 ? JsonMessage.getSuccess("添加成功") : JsonMessage.getFail("添加失败");
  }

  @Override
  public JsonMessage adminupdate(TbUserModel model) throws Exception {
    int result = tbUserDAO.adminupdate(model.getTbUser());
    // 添加日志
    TbLogs logs = new TbLogs();
    // 操作员
    logs.setOperator(model.getLoginAdminUser().getAuid().toString());
    // 类型
    logs.setLogtype("管理员修改");
    // 信息
    logs.setLog(String.format("修改了：%s", model.getTbUser()));
    tbLogsDAO.add(logs);
    return result == 1 ? JsonMessage.getSuccess("修改成功") : JsonMessage.getFail("修改失败");

  }
  
  @Override
  public JsonMessage userupdate(TbUserModel model) throws Exception{
    int result = tbUserDAO.userupdate(model.getTbUser());
    // 添加日志
    TbLogs logs = new TbLogs();
    // 操作员
    logs.setOperator(model.getLoginUser().getAuid().toString());
    // 类型
    logs.setLogtype("用户修改");
    // 信息
    logs.setLog(String.format("修改了：%s", model.getTbUser()));
    tbLogsDAO.add(logs);
    return result == 1 ? JsonMessage.getSuccess("修改成功") : JsonMessage.getFail("修改失败");
  }
  
  @Override
  public JsonMessage admindelete (TbUserModel model)throws Exception{
    model.getTbUser().setIsEnable("n");
    int result =tbUserDAO.admindelete(model.getTbUser());
    // 添加日志
    TbLogs logs = new TbLogs();
    // 操作员
    logs.setOperator(model.getLoginAdminUser().getAuid().toString());
    // 类型
    logs.setLogtype("管理员禁用");
    // 信息
    logs.setLog(String.format("禁用了:%s", model.getTbUser()));
    return result==1?JsonMessage.getSuccess("禁用成功"):JsonMessage.getFail("禁用失败");
  }
  
  @Override
  public JsonMessage nuadmindelete (TbUserModel model)throws Exception{
    model.getTbUser().setIsEnable("y");
    int result =tbUserDAO.admindelete(model.getTbUser());
    // 添加日志
    TbLogs logs = new TbLogs();
    // 操作员
    logs.setOperator(model.getLoginAdminUser().getAuid().toString());
    // 类型
    logs.setLogtype("管理员启用");
    // 信息
    logs.setLog(String.format("启用了:%s", model.getTbUser()));
    return result==1?JsonMessage.getSuccess("启用成功"):JsonMessage.getFail("启用失败");
  }
}
